package me.vudu.Conflict.Listeners.Skills;

import java.util.Random;

import me.vudu.Conflict.ConflictPlugin;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.SkullType;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerFishEvent;

import org.bukkit.inventory.ItemStack;
import org.getspout.spoutapi.player.SpoutPlayer;

/**
 * verarbeitet folgende Skills
 * 
 * Butcher 1-4 Ender 1
 * 
 * @author Vudu
 * 
 */
public class PlayerKillEntityListener implements Listener {

	ConflictPlugin plugin;
	Random generator;

	public PlayerKillEntityListener(ConflictPlugin plugin) {
		Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
		this.plugin = plugin;
		generator = new Random();
	}
	
	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerKillPlayerEvent(PlayerDeathEvent event) {
		SpoutPlayer killer;
		SpoutPlayer  killed = (SpoutPlayer) event.getEntity();;
		if (null != event.getEntity().getKiller() && event.getEntity().getKiller() instanceof SpoutPlayer) {
			killer = (SpoutPlayer) event.getEntity().getKiller();
			
			if(!ConflictPlugin.playerIsInTeam(killer).equals(ConflictPlugin.playerIsInTeam(killed))){
				killer.getWorld().dropItem(event.getEntity().getLocation(), new ItemStack(Material.SKULL_ITEM, 1, (short)SkullType.PLAYER.ordinal()));			
			}
		}
		
	}
		
	

	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerKillMonsterEvent(EntityDeathEvent event) {
		SpoutPlayer sp;

		if (null != event.getEntity().getKiller() && event.getEntity().getKiller() instanceof SpoutPlayer) {
			sp = (SpoutPlayer) event.getEntity().getKiller();
			
			
			
			if (event.getEntity().getType() == EntityType.ENDERMAN) {
				if (ConflictPlugin.skillFightDropEnder.get(sp.getName()) > 0) {
					sp.getWorld().dropItem(sp.getLocation(), new ItemStack(368));
					event.getDrops().clear();
				}
				// Logging
				ConflictPlugin.totalMobs.put(sp.getName(), ConflictPlugin.totalMobs.get(sp.getName()) + 1);

			}
			else if (event.getEntity().getType() == EntityType.PIG_ZOMBIE) {
				if (ConflictPlugin.skillFightDropNormal.get(sp.getName()) > 0) {
					sp.getWorld().dropItem(sp.getLocation(), new ItemStack(397));
					
				}else{
					if(generator.nextInt(2)>=1){
						sp.getWorld().dropItem(sp.getLocation(), new ItemStack(397));
					}
				}
				// Logging
				ConflictPlugin.totalMobs.put(sp.getName(), ConflictPlugin.totalMobs.get(sp.getName()) + 1);

			}else if (event.getEntity().getType() == EntityType.SKELETON) {
				if (ConflictPlugin.skillFightDropNormal.get(sp.getName()) > 0) {
					sp.getWorld().dropItem(sp.getLocation(), new ItemStack(352));
					
				}
				// Logging
				ConflictPlugin.totalMobs.put(sp.getName(), ConflictPlugin.totalMobs.get(sp.getName()) + 1);

			}else if (event.getEntity().getType() == EntityType.ZOMBIE || event.getEntity().getType() == EntityType.PIG_ZOMBIE) {
				if (ConflictPlugin.skillFightDropNormal.get(sp.getName()) > 0) {
					sp.getWorld().dropItem(sp.getLocation(), new ItemStack(367));
					
				}
				// Logging
				ConflictPlugin.totalMobs.put(sp.getName(), ConflictPlugin.totalMobs.get(sp.getName()) + 1);

			}
			else if (event.getEntity().getType() == EntityType.SPIDER || event.getEntity().getType() == EntityType.CAVE_SPIDER) {
				if (ConflictPlugin.skillFightDropNormal.get(sp.getName()) > 0) {
					sp.getWorld().dropItem(sp.getLocation(), new ItemStack(375));
				}
				// Logging
				ConflictPlugin.totalMobs.put(sp.getName(), ConflictPlugin.totalMobs.get(sp.getName()) + 1);

			}
			else if (event.getEntity().getType() == EntityType.COW
					|| event.getEntity().getType() == EntityType.CHICKEN
					|| event.getEntity().getType() == EntityType.PIG || event.getEntity().getType() == EntityType.SHEEP) {
			
				// Logging
				ConflictPlugin.totalAnimal.put(sp.getName(), ConflictPlugin.totalAnimal.get(sp.getName()) + 1);
			}else{
				// Logging
				ConflictPlugin.totalMobs.put(sp.getName(), ConflictPlugin.totalMobs.get(sp.getName()) + 1);
			}

		}

	}

	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerKillAnimalEvent(EntityDamageByEntityEvent event) {

		if (event.getEntity() instanceof LivingEntity) {

			LivingEntity ent = (LivingEntity) event.getEntity();
			if (event.getDamager() instanceof SpoutPlayer) {
				SpoutPlayer p = (SpoutPlayer) event.getDamager();

				if (event.getDamage() >= ent.getHealth()) {

					if (event.getEntity().getType() == EntityType.CHICKEN
							&& ConflictPlugin.skillFoodChicken.get(p.getName()) > 0) {

						p.getWorld().dropItem(p.getLocation(), new ItemStack(365));

					}
					if (event.getEntity().getType() == EntityType.COW
							&& ConflictPlugin.skillFoodCow.get(p.getName()) > 0) {

						p.getWorld().dropItem(p.getLocation(), new ItemStack(363));
					}
					if (event.getEntity().getType() == EntityType.PIG
							&& ConflictPlugin.skillFoodPork.get(p.getName()) > 0) {

						p.getWorld().dropItem(p.getLocation(), new ItemStack(319));

					}

				}
			}

		}
	}

	@EventHandler(priority = EventPriority.HIGH)
	public void onPlayerFishEvent(PlayerFishEvent event) {
		if (null != event.getCaught() && event.getCaught().getType() == EntityType.DROPPED_ITEM) {
			if (ConflictPlugin.skillFoodFish.get(event.getPlayer().getName()) > 0) {
				event.getPlayer().getWorld().dropItem(event.getPlayer().getLocation(), new ItemStack(349));
			}
		}
	}

}
